package com.bort.system.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bort.system.model.Category;
import com.bort.system.service.CategoryService;
import com.bort.system.utils.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/category")
public class AdminCategoryController {

    @Resource
    CategoryService categoryService;

    @RequestMapping("/list")
    public Result<Page<Category>> getCategoryList(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rowSize", defaultValue = "12") Integer rowSize,
            @RequestParam(value = "categoryName",required = false)String categoryName
    ) {

        Page<Category> pager = new Page<>();
        pager.setCurrent(page);
        pager.setSize(rowSize);
        Map<String,Object> map = new HashMap<>();
        map.put("categoryName",categoryName);
        return categoryService.getCategoryList(pager,map);
    }

    @RequestMapping("/add")
    public Result<Category> addCategory(@RequestBody Category category){
        if(category == null){
            return Result.error("数据有误");
        }
        int in = categoryService.addCategory(category);
        return Result.success("新增成功");
    }

    @RequestMapping("/update")
    public Result<Category> updateCategory(@RequestBody Category category){
        if(category == null){
            return Result.error("数据有误");
        }
        return  categoryService.updateCategory(category);
    }

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public Result upload(@RequestParam(value = "file",required = false) MultipartFile file){
        return categoryService.upload(file);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public Result delete(@RequestParam("categoryId") Integer categoryId){
        return categoryService.delete(categoryId);
    }


    @RequestMapping("/load")
    public Result<List<Category>> loadAllCategories() {
        return categoryService.loadAllCategories(new HashMap<>());
    }

}
